package dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import java.util.UUID;

import org.apache.catalina.tribes.util.UUIDGenerator;

import pojo.Order;
import pojo.User;
import util.DBUtil;
import dao.OrderDao;

public class OrderDaoImpl implements OrderDao{

	@Override
	public int addOrder(Order o) {
		Connection con=null;
		con=DBUtil.createConnection();
		PreparedStatement ps=null;
		ResultSet rs=null;
		String sql="insert into orders(id,u_id,b_id,count,buyTime)values(?,?,?,?,?)";
		int num=0;
		try {
			ps=con.prepareStatement(sql);
			ps.setString(1,UUID.randomUUID().toString());
			con=DBUtil.createConnection();
			ps.setString(2, o.getU_id());
			ps.setString(3, o.getB_id());
			ps.setDouble(4, o.getNumber());
			ps.setDate(5, new java.sql.Date(o.getBuyTime().getTime()));
			
			num= ps.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DBUtil.close(null, con, ps);
		}
		return num;
	}

	@Override
	public int addOrderList(List<Order> orders) {
		Connection con=null;
		con=DBUtil.createConnection();
		PreparedStatement ps=null;
		ResultSet rs=null;
		String sql="insert into orders(id,u_id,b_id,count,buyTime)values(?,?,?,?,?)";
		int n=0;
		try {
			ps=con.prepareStatement(sql);
			for(Order order:orders){
				ps.setString(1,UUID.randomUUID().toString());
				con=DBUtil.createConnection();
				ps.setString(2, order.getU_id());
				ps.setString(3, order.getB_id());
				ps.setDouble(4, order.getNumber());
				ps.setDate(5, new java.sql.Date(order.getBuyTime().getTime()));
			    ps.addBatch();
			}
			int ms[]=ps.executeBatch();
			n=ms.length;
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DBUtil.close(null, con, ps);
		}
		return n;
	}

}
